package com.zcx.webview;

import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;


import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class PlayActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
    private VideoView vv;
    private GestureDetector detector;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.fullscreen_play);
        vv=findViewById(R.id.vv);
        String url = getIntent().getStringExtra("url");
        System.out.println("url:"+url);
        vv.setVideoPath(url);
        MediaController controller = new MediaController(this);
        vv.setMediaController(controller);

        DisplayMetrics metrics=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        ViewGroup.LayoutParams params = vv.getLayoutParams();
        params.width=metrics.widthPixels;
        params.height=metrics.heightPixels;
        System.out.println(String.format("(%d,%d)",params.width,params.height));

        vv.setLayoutParams(params);

        vv.requestFocus();
        vv.setOnCompletionListener(this);
        vv.setOnErrorListener(this);
        vv.start();
        System.out.println("duration:"+vv.getDuration());
        setGestureDetector();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        vv.stopPlayback();
        vv=null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        vv.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        vv.stopPlayback();
    }

    //播放完成回调
    @Override
    public void onCompletion(MediaPlayer mp) {
        setResult(3);
        finish();
    }

    //设置手势
    private void setGestureDetector(){
        detector = new GestureDetector(this,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                        System.out.println(String.format("(distenceX,distenceY)=(%f,%f)",distanceX,distanceY));
                        System.out.println(String.format("(e1.X,e1.Y)=(%f,%f)",e1.getX(),e1.getY()));
                        System.out.println(String.format("(e2.X,e2.Y)=(%f,%f)",e2.getX(),e2.getY()));
                        if(Math.abs(distanceY)>Math.abs(distanceX)){//上下划
                            //int y= (int)(2*distanceY/vv.getLayoutParams().height);
                            int y=(int)Math.round(distanceY);
                            System.out.println("Y:"+y);
                            if(e1.getX()>vv.getLayoutParams().width/2){
                                setAudio(y);
                            }else {
                                setLightness(y);
                            }
                        }else {//左右划
                            //int x= (int)(2*distanceY/vv.getLayoutParams().width);
                            int x=-(int)Math.round(distanceX);
                            System.out.println("X:"+x);
                            setProccess(x);
                        }
                        return true;
                    }
                });

    }

    //设置视频进度
    public void setProccess(int proccess){
        int duration = vv.getDuration();
        int x=vv.getCurrentPosition()+proccess*10;
        if(x>duration){
            x=duration;
        }
        if(x<0){
            x=0;
        }
        vv.seekTo(x);
    }

    //改变屏幕亮度
    public void setLightness(float lightness){
        WindowManager.LayoutParams layoutParams =getWindow().getAttributes();
        //屏幕的亮度,最大是255
        layoutParams.screenBrightness =layoutParams.screenBrightness+lightness/255f;
        if(layoutParams.screenBrightness>1){
            layoutParams.screenBrightness=1;
        }else if(layoutParams.screenBrightness<0.2){
            layoutParams.screenBrightness=0.2f;
        }
        getWindow().setAttributes(layoutParams);
    }

    //设置音量
    public void setAudio(int volume) {
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        //当前音量
        int k = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        //最大音量
        int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        Log.d("audio", "max:" + max);
        Log.d("audio", "cur:" + k);
        k = k + volume/15;
        if (k >= 0 && k <= max) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, k, AudioManager.FLAG_PLAY_SOUND);
        } else {
            return;
        }
    }

    //发生错误回调
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        Intent intent = new Intent();
        intent.putExtra("what",what);
        intent.putExtra("extra",extra);
        setResult(4,intent);
        finish();
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
}
